<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Offer_model extends Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

   function list_offer($limit, $start)
    {  // echo "limit".$limit."start".$start;
        try{
            $offer_sql = "select a.offer_id,
                    (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='UK' and module='offer') as title ,
                    (select lvalue from tbl_lang b where b.lkey=a.desc and b.lang='UK' and module='offer') as `desc` ,
                    a.offer_start,
                    a.offer_end,
                    a.`status`
                    from tbl_offer a order by offer_id, title
                          ";
           $offer_sql.= " limit ".$start.",".$limit;
            $offers = $this->db->query($offer_sql);
            if($offers->num_rows() > 0) {
                 return $offer_result = $offers->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
  function list_offer_count()
    {
        try{
            $offer_sql = "select  count(*) as count
                    from tbl_offer  
                          ";

            $offers = $this->db->query($offer_sql);
            if($offers->num_rows() > 0) {
                  $offer_result = $offers->row_array();
                  return $offer_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    function saveoffer($post_arr, $langarr)
    {
        $this->load->database();
    	$offerdetailsarray=array();
        $last_insert_id=-1;
        $offerdetailsarray["offer_type_id"]=$post_arr['offer_type_id'];
        $offerdetailsarray["hotel_id"]=$post_arr['hotel_id'];
        $offerdetailsarray["offer_start"]=$post_arr['offer_start'];
        $offerdetailsarray["offer_end"]=$post_arr['offer_end'];
        $offerdetailsarray["status"]=$post_arr['status'];
        
        $sql = "INSERT INTO tbl_offer(offer_type_id, hotel_id, offer_start, offer_end, status)
                VALUES('" . $offerdetailsarray["offer_type_id"] . "', '" . $offerdetailsarray["hotel_id"] . "', '" . $offerdetailsarray["offer_start"] . "', '" . $offerdetailsarray["offer_end"] . "', '" . $offerdetailsarray["status"] . "')";
       $query = $this->db->query($sql);
    	if($query)
    	{
            $last_insert_id = $this->db->insert_id();
            $offer_title_lang_key = $last_insert_id . "_title";
            $offer_desc_lang_key = $last_insert_id . "_desc";

            $sql = "UPDATE tbl_offer SET title = '" . $offer_title_lang_key . "', `desc` = '" . $offer_desc_lang_key . "'
                    WHERE offer_id = " . $last_insert_id;
            $query = $this->db->query($sql);
            if($query)
            {
                foreach ($langarr as $langkey=>$langval)
                {
                    $offerdetailsarray['title'.$langkey]=$post_arr['title'.$langkey];
                    $offerdetailsarray['desc'.$langkey]=$post_arr['desc'.$langkey];
                    $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . $offer_title_lang_key . "', '" . $langkey . "', '" . $offerdetailsarray["title" . $langkey] . "', " . $last_insert_id . ", 'offer')";
                    $query = $this->db->query($sql);
                    if($query)
                    {
                        $sql = "INSERT INTO tbl_lang(lkey, lang, lvalue, mod_id, module)
                            VALUES('" . $offer_desc_lang_key . "', '" . $langkey . "', '" . $offerdetailsarray["desc" . $langkey] . "', " . $last_insert_id . ", 'offer')";
                        $query = $this->db->query($sql);
                        
                    }
                }
            }
        }
        return $last_insert_id;
           
    }
    
    function get_offer_details_by_id($offer_id)
    {
        try {
            $offer_sql = "SELECT *
                FROM tbl_offer 
                WHERE offer_id = ?";
            $offers = $this->db->query($offer_sql, array($offer_id));
            if($offers->num_rows() > 0) {
                 return $offer_result = $offers->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    } 
    
    function get_offer_details_lang_by_id($lkey, $langkey, $module='offer')
    {
        $sql = "SELECT * FROM tbl_lang WHERE lkey = ? and lang=? and module=?";
        $facilty = $this->db->query($sql, array($lkey, $langkey, $module));
        if($facilty->num_rows() > 0) {
             return $fac_result = $facilty->result_array();
        } else {
            return FALSE;
        }
    }

    function get_hotel_list($hotelid)
    {
        if ($hotelid){
            $where=" and a.id={$hotelid} ";
        }
        $sql = "select *,a.id as hotel_id,
                    (select lvalue from tbl_lang b where b.lkey=a.hotelname and b.lang='UK') as hotelname                     
                    from tbl_hotelmaster a where status=1 {$where} order by hotelname";
        $hotels = $this->db->query($sql);
        if($hotels->num_rows() > 0) {
             return $$hotel_result = $hotels->result_array();
        } else {
            return FALSE;
        }
    }

    function updateoffer($post_arr, $data)
    {
        //print_r($post_arr['facility']);
        $sql = "UPDATE tbl_offer
                SET offer_type_id='".$post_arr['offer_type_id']."', hotel_id='".$post_arr['hotel_id']."', offer_start = '" . $post_arr['offer_start'] . "', offer_end = '" . $post_arr['offer_end'] . "', status = '" . $post_arr['status'] . "'
                WHERE offer_id = " . $post_arr['offer_id'];
        
        $query = $this->db->query($sql);
        if($query)
        {
            foreach($data['partnerlang'] as $langkey=>$langval)
            {
                $sql_name = "UPDATE tbl_lang SET lvalue = '" . $post_arr['title' . $langkey] . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['offer_id'] . "
                                AND lkey = '" . $post_arr['offer_id'].'_title' . "'
                                    AND module = 'offer' ";
                
                $query_title = $this->db->query($sql_name);

                $sql_desc = "UPDATE tbl_lang SET lvalue = '" . $post_arr['desc' . $langkey] . "'
                        WHERE lang='" . $langkey . "'
                            AND mod_id = " . $post_arr['offer_id'] . "
                                AND lkey='".$post_arr['offer_id'].'_desc'."'
                                    AND module = 'offer';";
                
                $query_desc = $this->db->query($sql_desc);
            }

            
        }
    }

    function delete_offer($offer_id)
    {
        $del_offer_sql = "DELETE FROM tbl_offer WHERE offer_id = " . $offer_id;
        $offer_qry = $this->db->query($del_offer_sql);
        if($offer_qry)
        {            
            $lang_sql = "DELETE FROM tbl_lang WHERE mod_id = " . $offer_id . " AND module='offer' ";
            $lang_qry = $this->db->query($lang_sql);            
        }
    }
    
}
?>
